﻿using System;
using System.Collections.Generic;
using System.Text;
using Telerik.Web.UI;
using System.Web;
using Acms.Bll;
using System.Web.UI.WebControls;

namespace Acms.AcmsControls.ContentControls.Admin
{
    public interface ISpellCheck
    {
        bool PerformSpellCheck{get;}
        string ClientSpellTextSource { get; }
        string[] ControlsToSpellCheck { get; }
    }
    public class HtmlAdminEdit : BaseAdminEdit<HtmlContentControlProperties>, ISpellCheck
    {
        protected override string GetCssClass
        {
            get { return "html"; }
        }

        protected Button btnSwitch = new Button();
        protected Panel radPanel = new Panel();
        protected RadEditor radEditor = new RadEditor();
        protected Panel wymPanel = new Panel();
        protected TextBox wymEditor = new TextBox();
        //protected RadSpell wymSpell = new RadSpell();

        protected override void CreateChildControls()
        {
            this.Controls.Add(btnSwitch);
            btnSwitch.Click += new EventHandler(btnSwitch_Click);

            radEditor.NewLineBr = false;
            radEditor.Language = System.Globalization.CultureInfo.CurrentCulture.Name;
            radEditor.SpellCheckSettings.DictionaryLanguage = System.Globalization.CultureInfo.CurrentCulture.Name;
            radEditor.ToolsFile = Page.ResolveUrl("~/Posts/RadEditorTools.xml");
            //acms location editor.CssFiles.Add("~/../acms/css/RadEditorStyles.css");
            //website location editor.CssFiles.Add("~/../Styles/RadEditorStyles.css");

            //radEditor.Paragraphs.Add("<p class='radEditor'>Paragraph<p>", "<p>");
            //radEditor.Paragraphs.Add("<h1>Heading 1<h1>", "<h1>");
            //radEditor.Paragraphs.Add("<h2>Heading 2<h2>", "<h2>");
            //radEditor.Paragraphs.Add("<h3>Heading 3<h3>", "<h3>");


            var csspath = Page.Server.MapPath("~/../acms/css/RadEditorStyles.css");
            var cssthere = System.IO.File.Exists(csspath);
            if (cssthere == true){
                radEditor.CssFiles.Add("~/../acms/css/RadEditorStyles.css");
            }
            radEditor.StripFormattingOptions = EditorStripFormattingOptions.MSWordRemoveAll; ;
            radPanel.Controls.Add(radEditor);
            this.Controls.Add(radPanel);

            wymEditor.ID = "wymEditor";
            wymEditor.TextMode = TextBoxMode.MultiLine;
            //wymEditor.Rows = 20;
            //wymEditor.Columns = 50;

            //wymSpell.ButtonText = "Spell Check";
            //wymSpell.ButtonType = Telerik.Web.UI.ButtonType.LinkButton;
            //wymSpell.DictionaryLanguage = "en-GB";
            //wymSpell.ControlToCheck = btnSwitch.ClientID;
            //wymSpell.IsClientID = true;
            //wymSpell.ClientTextSource = string.Format("{0}_SpellCheckSource_Error", this.ClientID);

            //wymPanel.Controls.Add(wymSpell);
            wymPanel.Controls.Add(wymEditor);
            this.Controls.Add(wymPanel);

            if (Page.Request.Cookies["Editor"] == null || Page.Request.Cookies["Editor"].Value == "wym")
            {
                wymPanel.Visible = true;
                radPanel.Visible = false;
                btnSwitch.Text = "Switch to Advanced Control";
            }
            else
            {
                wymPanel.Visible = false;
                radPanel.Visible = true;
                btnSwitch.Text = "Switch to Simple Control";
            }

            base.CreateChildControls();
        }

        void btnSwitch_Click(object sender, EventArgs e)
        {
            if (wymPanel.Visible)
            {
                wymPanel.Visible = false;
                radPanel.Visible = true;
                radEditor.Content = wymEditor.Text;
                btnSwitch.Text = "Switch to Simple Control";
            }
            else
            {
                wymPanel.Visible = true;
                radPanel.Visible = false;
                wymEditor.Text = radEditor.Content;
                btnSwitch.Text = "Switch to Advanced Control";
            }
            HttpCookie cookie = Page.Request.Cookies["Editor"];
            if (cookie == null)
            {
                cookie = new HttpCookie("Editor");
            }
            cookie.Value = wymPanel.Visible ? "wym" : "rad";
            Page.Response.Cookies.Set(cookie);
        }

        protected override void SaveToProperties(HtmlContentControlProperties properties, PostControl control)
        {
            if (wymPanel.Visible)
            {
                properties.SetHtml(wymEditor.Text);
            }
            else
            {
                properties.SetHtml(radEditor.Content);
            }
        }

        public override void PropertiesLoaded(HtmlContentControlProperties properties)
        {
            EnsureChildControls();
            if (properties != null)
            {
                radEditor.Content = properties.GetHtml();
                wymEditor.Text = properties.GetHtml();
            }
        }

       protected override void OnPreRender(EventArgs e)
       {
            if (wymPanel.Visible)
            {
                string javasctiptSpellCheckClass = @"
                function HtmlSpellTextSource() 
                {
                    this.get_text = function() {return $.wymeditors(0).html();}
                    this.set_text = function(newValue) {$.wymeditors(0).html(newValue);}
                }";
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "SpellTextSource", javasctiptSpellCheckClass, true);

                StringBuilder javascript = new StringBuilder();
                javascript.AppendFormat(@"
                $(document).ready(function(){{
                    $('#{0}').wymeditor();
                    $(document.forms[0]).submit(function(){{
                        if (!wymEditorSubmit()) {{
                            return false;
                        }}
                    }});
                    $(""a"").each(function(i) {{
                        if (this.href.substr(0, 24) == ""javascript:__doPostBack("") {{
                            $(this).click(function() {{
                                if (!wymEditorSubmit()) {{
                                    return false;
                                }}
                            }});
                        }}
                    }});
                }});

                var wymEditorSubmit = function() {{
                    try {{
                        for (var i = 0; i < $.wymeditors.length; i++) {{
                            $.wymeditors(i).update();
                        }}
                    }}
                    catch (e) {{
                        alert(e);
                        return false;
                    }}
                    return true;
                }}
                ", wymEditor.ClientID);

                javascript.AppendFormat("var {0} = new HtmlSpellTextSource();", this.ClientSpellTextSource);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), string.Concat(this.ClientID, "_StartUp"), javascript.ToString(), true);
            }
            base.OnPreRender(e);
        }

       #region ISpellCheck Members

       public bool PerformSpellCheck
       {
           get { return this.wymPanel.Visible; }
       }

       public string ClientSpellTextSource
       {
           get { return string.Format("{0}HtmlSpellTextSource", this.ClientID); }
       }

       public string[] ControlsToSpellCheck
       {
           get { return null; }
       }

       #endregion
    }
}
